<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class CommonUpload extends ApiHomeController {

	function __construct() { 
		parent::__construct();

	}

	function upload($type){
		$director = "upload/".((new UploadType)->showByIndex($type,XPHEnum::Remark))."/";
		$output_dir = $director.date('Ymd',time())."/";
		if(!file_exists($output_dir))
		{
			mkdir($output_dir, 0777);
		}
		if(isset($_FILES["myfile"]))
		{
			if(!is_array($_FILES["myfile"]['name'])){
				$error =$_FILES["myfile"]["error"];
				$type=explode('.',$_FILES["myfile"]['name']);
				$document_name=buildOrdeNo();
			 	$fileName = $document_name.'.'.$type[count($type)-1];
			 	//确定icon
			 	$icon='other.png';
			 	foreach ((new CommonDocument)->getAll() as $k => $v) {
			 		if(in_array($type[count($type)-1],$v[XPHEnum::Caption])){
			 			$icon=$v[XPHEnum::Remark];
			 			break;
			 		}
			 	}
			 	if($icon!=CommonDocument::Img[XPHEnum::Remark]){
			 		exit(retJson('上传的文件必须为图片类型：'.implode(',', CommonDocument::Img[XPHEnum::Caption]),false));
			 	}
			 	move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir.$fileName);
			 	$path=$output_dir.$fileName;
			 	exit(retJson('',true,$path,$icon));
			}
			else{
				$path=array();
				$icon=array();
				foreach ($_FILES["myfile"]['name'] as $k1 => $v1) {
					$error =$_FILES["myfile"]["error"][$k1];
					$type=explode('.',$_FILES["myfile"]['name'][$k1]);
					$document_name=buildOrdeNo();
				 	$fileName = $document_name.'.'.$type[count($type)-1];
				 	//确定icon
				 	$icon_item='other.png';
				 	foreach ((new CommonDocument)->getAll() as $k => $v) {
				 		if(in_array($type[count($type)-1],$v[XPHEnum::Caption])){
				 			$icon_item=$v[XPHEnum::Remark];
				 			$icon[]=$icon_item;
				 			break;
				 		}
				 	}
				 	if($icon_item!=CommonDocument::Img[XPHEnum::Remark]){
				 		exit(retJson('上传的文件必须为图片类型：'.implode(',', CommonDocument::Img[XPHEnum::Caption]),false));
				 	}
				 	move_uploaded_file($_FILES["myfile"]["tmp_name"][$k1],$output_dir.$fileName);
				 	$path[]=$output_dir.$fileName;
				}
				exit(retJson('',true,$path,$icon));
			}
		}
		else{
			exit(retJson('上传失败',false));
		}
	}

	function uploadFile($type){
		$director = "upload/".((new UploadType)->showByIndex($type,XPHEnum::Remark))."/";
		$output_dir = $director.date('Ymd',time())."/";
		if(!file_exists($output_dir))
		{
			mkdir($output_dir, 0777);
		}
		if(isset($_FILES["myfile"]))
		{
			if(!is_array($_FILES["myfile"]['name'])){
				$error =$_FILES["myfile"]["error"];
				$type=explode('.',$_FILES["myfile"]['name']);
				$document_name=buildOrdeNo();
			 	$fileName = $document_name.'.'.$type[count($type)-1];
			 	//确定icon
			 	$icon='other.png';
			 	foreach ((new CommonDocument)->getAll() as $k => $v) {
			 		if(in_array($type[count($type)-1],$v[XPHEnum::Caption])){
			 			$icon=$v[XPHEnum::Remark];
			 			break;
			 		}
			 	}
			 	// if($icon!=CommonDocument::Excel[XPHEnum::Remark]){
			 	// 	exit(retJson('上传的文件必须为Excel类型：'.implode(',', CommonDocument::Img[XPHEnum::Caption]),false));
			 	// }
			 	move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir.$fileName);
			 	$path=$output_dir.$fileName;
			 	exit(retJson('',true,$path,$icon));
			}
			else{
				$path=array();
				$icon=array();
				foreach ($_FILES["myfile"]['name'] as $k1 => $v1) {
					$error =$_FILES["myfile"]["error"][$k1];
					$type=explode('.',$_FILES["myfile"]['name'][$k1]);
					$document_name=buildOrdeNo();
				 	$fileName = $document_name.'.'.$type[count($type)-1];
				 	//确定icon
				 	$icon_item='other.png';
				 	foreach ((new CommonDocument)->getAll() as $k => $v) {
				 		if(in_array($type[count($type)-1],$v[XPHEnum::Caption])){
				 			$icon_item=$v[XPHEnum::Remark];
				 			$icon[]=$icon_item;
				 			break;
				 		}
				 	// }
				 	// if($icon_item!=CommonDocument::Excel[XPHEnum::Remark]){
				 	// 	exit(retJson('上传的文件必须为Excel类型：'.implode(',', CommonDocument::Img[XPHEnum::Caption]),false));
				 	}
				 	move_uploaded_file($_FILES["myfile"]["tmp_name"][$k1],$output_dir.$fileName);
				 	$path[]=$output_dir.$fileName;
				}
				exit(retJson('',true,$path,$icon));
			}
		}
		else{
			exit(retJson('上传失败',false));
		}
	}


	function delete(){
		if(file_exists($_POST['path'])){
			unlink($_POST['path']);
		}
		exit(retJson('删除成功',true));
	}
}